name: UI tests

on:
  pull_request:
    types: [opened, synchronize]
    branches:
      - 'dev'
    paths:
      - '.github/workflows/tests-on-pr.yml'
      - 'ui/**/*.js'
      - 'ui/package.json'
      - 'ui/dev/package.json'

jobs:
  build:
    permissions:
      contents: read # to fetch code (actions/checkout)

    runs-on: ubuntu-latest

    # Check if the build cache exists, and if it does, do nothing else.
    # Otherwise, run the build and cache it.
    name: Build the packages
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v3

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm i

      - name: Build the UI package
        working-directory: ui
        run: pnpm build

      - name: Upload UI build artifact
        uses: actions/upload-artifact@v4
        with:
          name: ui-build
          path: ui/dist

      - name: Build vite-plugin package
        working-directory: vite-plugin
        run: pnpm build

      - name: Upload vite-plugin build artifact
        uses: actions/upload-artifact@v4
        with:
          name: vite-plugin-build
          path: vite-plugin/dist

  tests:
    # If the build pipeline doesn't succeed, we don't need to run tests at all
    needs: build

    permissions:
      contents: read # to fetch code (actions/checkout)
      actions: read # to correctly identify workflow run (cypress-io/github-action)

    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ui

    strategy:
      fail-fast: false
      matrix:
        browser: [chrome, firefox]

    name: Tests on ${{ matrix.browser }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v3

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm i

      # We have to cache Cypress binary as well (https://on.cypress.io/not-installed-ci-error)
      - name: Get Cypress version
        id: cypress-version
        working-directory: ui
        run: |
          echo "CYPRESS_VERSION=$(pnpm why cypress --depth 0 --json | jq -r '.[0].devDependencies.cypress.version')" > $GITHUB_OUTPUT
      - name: Cache Cypress binary
        id: cypress-cache
        uses: actions/cache@v4
        with:
          # https://docs.cypress.io/guides/references/advanced-installation#Skipping-installation
          path: ~/.cache/Cypress
          key: cypress-cache-${{ steps.cypress-version.outputs.CYPRESS_VERSION }}
          restore-keys: |
            cypress-cache-
      - name: Install Cypress
        if: steps.cypress-cache.outputs.cache-hit != 'true'
        run: pnpm cypress install

      - name: Download UI build artifact
        uses: actions/download-artifact@v4
        with:
          name: ui-build
          path: ui/dist

      - name: Download vite-plugin build artifact
        uses: actions/download-artifact@v4
        with:
          name: vite-plugin-build
          path: vite-plugin/dist

      - name: Run tests in ${{ matrix.browser }} browser
        uses: cypress-io/github-action@v6
        env:
          CYPRESS_JUNIT_RESULTS_FILENAME: test-results/${{ matrix.browser }}/[hash].xml
        with:
          install: false
          command: pnpm test:component:run --browser ${{ matrix.browser }}
          working-directory: ui

      # Upload required artifacts to be used in tests-on-pr-report.yml

      - name: Upload test results
        if: ${{ always() }}
        uses: actions/upload-artifact@v4
        with:
          name: test-results-${{ matrix.browser }}
          path: ui/dev/test-results

      - name: Upload GitHub Actions event data
        if: ${{ always() }}
        uses: actions/upload-artifact@v4
        with:
          name: event-data
          path: ${{ github.event_path }}
          overwrite: true
